@echo off
echo Starting to package extension...

:: Create distribution directory
if not exist "dist" mkdir dist

:: Clean old distribution files
del /Q /S dist\*

:: Copy necessary files
xcopy /Y manifest.json dist\
xcopy /Y background.js dist\
xcopy /Y popup.html dist\
xcopy /Y popup.js dist\
xcopy /Y /E /I images dist\images
xcopy /Y LICENSE dist\
xcopy /Y LICENSE.zh-CN dist\
xcopy /Y README.md dist\
xcopy /Y README.en.md dist\

:: Generate private key (if not exists)
if not exist "dist\private.pem" (
    echo Generating new private key...
    openssl genrsa -out "dist\private.pem" 2048
)

:: Package as zip file for Edge Store submission
cd dist
powershell Compress-Archive -Path * -DestinationPath auto-rewards.zip -Force
cd ..

:: Try to find Chrome browser
set "CHROME_PATH="

:: Check common Chrome installation paths
if exist "C:\Program Files\Google\Chrome\Application\chrome.exe" (
    set "CHROME_PATH=C:\Program Files\Google\Chrome\Application\chrome.exe"
) else if exist "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" (
    set "CHROME_PATH=C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
)

:: Package with Chrome if available
if defined CHROME_PATH (
    echo Packaging CRX using Chrome...
    "%CHROME_PATH%" --pack-extension="%CD%\dist" --pack-extension-key="%CD%\dist\private.pem"
    if exist dist.crx (
        move /Y dist.crx dist\auto-rewards.chrome.crx
        echo - Chrome CRX created successfully
    )
)

echo.
echo Packaging completed, files located at:
echo - ZIP file: dist\auto-rewards.zip (for Edge Store submission)
if exist "dist\auto-rewards.chrome.crx" echo - Chrome CRX file: dist\auto-rewards.chrome.crx
echo - Private key: dist\private.pem (please keep it safe)

if not defined CHROME_PATH echo Warning: Chrome browser not found, skipping Chrome CRX packaging

echo.
echo Note: For Edge Store submission, please:
echo 1. Visit https://partner.microsoft.com/dashboard/microsoftedge/overview
echo 2. Create a new submission
echo 3. Upload the ZIP file
echo 4. Fill in required information
echo 5. Submit for review

pause 